﻿@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section scripts{
        <script src="~/Areas/Mms/ViewModels/mms.com.js"></script>
        <script src="~/Areas/Mms/ViewModels/mms.viewModel.edit.js"></script>
        <script type="text/javascript">
            var data = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));
            ko.bindingViewModel(new viewModel(data));
            function viewModel(data)
            {
                var self = this;
                mms.viewModel.edit.apply(self, arguments);
                this.grid.size = { w: 6, h: 151};
                this.grid.OnAfterCreateEditor = function(editors,row){
                    com.readOnlyHandler('input')(editors["OperateNum"].target, true);
                    com.readOnlyHandler('input')(editors["OperateMoney"].target, true);
                    var fnCalc = function(){
                        editors["OperateNum"].target.numberbox('setValue',editors["ActualNum"].target.numberbox('getValue')-row.BookNum);
                        editors["OperateMoney"].target.numberbox('setValue',editors["ActualNum"].target.numberbox('getValue') * editors["ActualUnitPrice"].target.numberbox('getValue') -row.BookMoney);
                    };
                    editors["ActualNum"].target.blur(fnCalc);
                    editors["ActualUnitPrice"].target.blur(fnCalc);

                    var BookMoney=0; 
                    var OperateMoney = Number(editors["OperateMoney"].target.numberbox('getValue'))  - Number(editors["OperateMoney"].oldHtml.replace(',', '') * 100) / 100
                    $.each(self.grid.datagrid('getData').rows, function () {
                        BookMoney += Number(this.BookMoney);
                        OperateMoney +=Number(this.OperateMoney);
                    });
                    self.form.BookMoney(BookMoney);
                    self.form.OperateMoney(OperateMoney);
                };
                this.addRowClick = function(){
                    if (self.readonly()) return;
                    var param = {_xml:"mms.material_check",WarehouseCode:self.form.WarehouseCode()};
                    mms.com.selectMaterial(self,param);
                };
                this.removeRowClick = function () {
                    if (self.readonly()) return;
                    self.gridEdit.deleterow();
                    var BookMoney=0; 
                    var OperateMoney =0;
                    $.each(self.grid.datagrid('getData').rows, function () {
                        BookMoney += Number(this.BookMoney);
                        OperateMoney +=Number(this.OperateMoney);
                    });
                    self.form.BookMoney(BookMoney);
                    self.form.OperateMoney(OperateMoney);
                };
            }
        </script>
}

    <div class="z-toolbar">
        <a id="a_save" href="#" plain="true" class="easyui-linkbutton" icon="icon-save" data-bind="click:saveClick,linkbuttonDisable:readonly" title="保存">保存</a>
        <a id="a_undo" href="#" plain="true" class="easyui-linkbutton" icon="icon-undo" data-bind="click:rejectClick,linkbuttonDisable:readonly" title="撤消">撤消</a>
        <a id="a_audit" href="#" plain="true" class="easyui-linkbutton" icon="icon-user-accept" data-bind="click:auditClick,easyuiLinkbutton:approveButton" title="审核">审核</a>
        <a id="a_printer" href="#" plain="true" class="easyui-linkbutton" icon="icon-printer" title="打印" data-bind="click:printClick">打印</a>
        <div class="datagrid-btn-separator"></div>
        <a id="a_other" href="#" class="easyui-splitbutton" data-options="menu:'#divother',iconCls:'icon-application_go'" title="其他">其他</a>
        <div class="datagrid-btn-separator"></div>
        <a id="a_first" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_first" data-bind="click:firstClick,linkbuttonEnable:scrollKeys.firstEnable" title="第一条"></a> 
        <a id="a_previous" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_previous" data-bind="click:previousClick,linkbuttonEnable:scrollKeys.previousEnable" title="上一条"></a> 
        <a id="a_next" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_next" data-bind="click:nextClick,linkbuttonEnable:scrollKeys.nextEnable" title="下一条"></a> 
        <a id="a_last" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_last" data-bind="click:lastClick,linkbuttonEnable:scrollKeys.lastEnable" title="最后一条"></a> 
    </div>

    <div id="divother" style="width:100px; display:none;">
        <div id="a_add" data-options="iconCls:'icon-add'">新增</div>
        <div id="a_del" data-options="iconCls:'icon-cross'">删除</div>
        <div id="a_refresh" data-options="iconCls:'icon-arrow_refresh'">刷新</div>
    </div>  

    <div id="master" class="container_12" data-bind="inputwidth:0.9">
        <div class="grid_1 lbl">单据编号</div>
        <div class="grid_3 val"><input type="text" id="BillNo" name="BillNo"  data-bind="value:form.BillNo,readOnly:true" class="z-txt readonly"/></div>
        <div class="grid_1 lbl">单据日期</div>
        <div class="grid_3 val"><input type="text" id="BillDate" name="BillDate" data-bind="dateboxValue:form.BillDate,dateboxReadOnly:readonly" class="z-txt easyui-datebox" /></div>
        <div class="grid_1 lbl">经办人</div>
        <div class="grid_3 val "><input type="text" id="DoPerson" name="DoPerson" data-bind="value:form.DoPerson,readOnly:readonly" class="z-txt easyui-validatebox" /></div>
        
        <div class="clear"></div>

        <div class="grid_1 lbl">库房</div>
        <div class="grid_3 val"><input type="text" data-bind="datasource:dataSource.warehouseItems,comboboxValue:form.WarehouseCode,comboboxReadOnly:readonly" class="z-txt easyui-combobox"/></div>
        <div class="grid_1 lbl required">年份</div>
        <div class="grid_3 val"><input type="text" data-bind="datasource:dataSource.yearItems,comboboxValue:form.Years,comboboxReadOnly:readonly" class="z-txt easyui-combobox" required="true"  /></div>
        <div class="grid_1 lbl required">月份</div>
        <div class="grid_3 val"><input type="text" data-bind="datasource:dataSource.monthItems,comboboxValue:form.Months,comboboxReadOnly:readonly"  class="z-txt easyui-combobox" required="true"/></div>

        <div class="clear"></div>

        <div class="grid_1 lbl">帐面金额 </div>
        <div class="grid_3 val"><input type="text" data-bind="numberboxValue:form.BookMoney,readOnly:true" class="z-txt easyui-numberbox readonly" data-options="precision: 2"/></div>
        <div class="grid_1 lbl">盈亏金额</div>
        <div class="grid_3 val"><input type="text" data-bind="numberboxValue:form.OperateMoney,readOnly:true" class="z-txt easyui-numberbox readonly" data-options="precision: 2"/></div>
        <div class="grid_1 lbl">备注 </div>
        <div class="grid_3 val"><input type="text" id="Remark" name="Remark" data-bind="value:form.Remark,readOnly:readonly" class="z-txt" /></div>
       
        <div class="clear"></div>
    </div>
 
    <div id="tt" class="easyui-tabs">  
        <div title="材料明细">
            <table id="list" data-bind="datagrid:grid">
                <thead>
                    <tr>
                        <th field="BillNo" hidden="true"></th>  
                        <th field="RowId"  hidden="true" ></th> 
                        <th field="MaterialCode"        sortable="true" align="left"    width="80" >材料编码   </th>  
                        <th field="MaterialName"        sortable="true" align="left"    width="120" >材料名称   </th>  
                        <th field="Model"             sortable="true" align="center"    width="60" >规格型号       </th>  
      @*                  <th field="Material"            sortable="true" align="left"    width="80"  >材质   </th> *@  
                        <th field="Unit"                sortable="true" align="center"    width="60" editor1="{type: 'combobox', options:{data:data.dataSource.measureUnit}}">单位       </th>  
                        <th field="BookNum"             sortable="true" align="right"   width="70" >帐面数量       </th>  
                        <th field="BookUnitPrice"       sortable="true" align="right"   width="70" formatter="com.formatMoney"   >帐面单价       </th>  
                        <th field="BookMoney"           sortable="true" align="right"   width="70" formatter="com.formatMoney"   >帐面金额       </th>  
                        <th field="ActualNum"           sortable="true" align="right"   width="70"  editor="{type: 'numberbox',options:{min: 0, precision: 2}}">实际数量       </th>  
                        <th field="ActualUnitPrice"     sortable="true" align="right"   width="70"  editor="{type: 'numberbox',options:{min: 0, precision: 2}}"  formatter="com.formatMoney"   >实际单价       </th>  
                        <th field="OperateNum"          sortable="true" align="right"   width="70"  editor="{type: 'numberbox'}">盈亏数量       </th>  
                        <th field="OperateMoney"        sortable="true" align="right"   width="70"  editor="{type: 'numberbox',options:{precision: 2}}"  formatter="com.formatMoney"   >盈亏金额       </th>  
                        <th field="Remark"              sortable="true" align="left"    width="200" editor="text">备注       </th>   
                    </tr>
                </thead>
            </table> 
            
            <div id="gridtb">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" data-bind="click:addRowClick,linkbuttonDisable:readonly">选择在库材料</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" data-bind="click:removeRowClick,linkbuttonDisable:readonly">删除材料</a>
            </div>    
        </div>
        <div title="表单信息" class="hide" style="padding-top:2px;">
            <div class="container_12" id="BillDetail" data-bind="inputwidth:0.9,autoheight:181"> 
                <div class="clear"></div>
                <div class="grid_1 lbl">审批状态</div>
                <div class="grid_3 val"><input type="text" id="ApproveState" data-bind="value:form.ApproveState,readOnly:true" class="z-txt readonly"/></div>
                <div class="grid_1 lbl">审批意见</div>
                <div class="grid_3 val"><input type="text" id="ApproveRemark" data-bind="value:form.ApproveRemark,readOnly:true"  class="z-txt readonly"/></div>
                <div class="grid_1 lbl">审批人 </div>
                <div class="grid_3 val"><input type="text" id="ApprovePerson"data-bind="value:form.ApprovePerson,readOnly:true" class="z-txt readonly"/></div>

                <div class="clear"></div>
                <div class="grid_1 lbl">审批日期</div>
                <div class="grid_3 val"><input type="text" id="ApproveDate" data-bind="dateboxValue:form.ApproveDate,dateboxReadOnly:true"  class="z-txt easyui-datebox readonly"/></div>
                <div class="grid_1 lbl">编制日期</div>
                <div class="grid_3 val"><input type="text" id="CreateDate" data-bind="dateboxValue:form.CreateDate,dateboxReadOnly:true" class="z-txt easyui-datebox readonly" /></div>
                <div class="grid_1 lbl">编制人</div>
                <div class="grid_3 val "><input type="text" id="CreatePerson" data-bind="value:form.CreatePerson,readOnly:true" class="z-txt readonly" /></div>
                
                <div class="clear"></div>
                <div class="grid_1 lbl">修改日期</div>
                <div class="grid_3 val"><input type="text" id="UpdateDate" data-bind="dateboxValue:form.UpdateDate,dateboxReadOnly:true" class="z-txt easyui-datebox readonly" /></div>
                <div class="grid_1 lbl">修改人</div>
                <div class="grid_3 val "><input type="text" id="UpdatePerson" data-bind="value:form.UpdatePerson,readOnly:true" class="z-txt readonly" /></div>
            </div>
        </div> 
    </div>  
